package com.xinshujia.configuration;

import com.google.gson.GsonBuilder;
import com.xinshujia.configuration.adapter.DateAdapter;
import com.xinshujia.configuration.adapter.LocalDateAdapter;
import com.xinshujia.configuration.adapter.LocalDateTimeAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.GsonHttpMessageConverter;

import java.time.LocalDate;
import java.util.Date;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-07-11 13:05       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 * Gson 解析时间格式时的配置
 */
@Configuration
public class GsonConfig {
    @Bean
    public GsonHttpMessageConverter gsonHttpMessageConverter() {
        GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(Date.class, new DateAdapter());
        gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateAdapter());
        gsonBuilder.registerTypeAdapter(LocalDateTimeAdapter.class, new LocalDateTimeAdapter());
        gsonHttpMessageConverter.setGson(gsonBuilder.create());
        return gsonHttpMessageConverter;
    }
}
